package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    public String list(QueryObject qo, Model model){
        model.addAttribute("pageInfo",roleService.query(qo));
        return "role/list";
    }

    @RequestMapping("/input")
    public String input(Long id, Model model){
        model.addAttribute("role_permissions",roleService.selectPermissions(id));
        model.addAttribute("role",roleService.selectById(id));
        model.addAttribute("permissions",permissionService.selectAll());
        return "role/input";
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Role role, Long[] ids){
        if (role.getId() != null){
            roleService.update(role,ids);
        }else {
            roleService.insert(role,ids);
        }
        return "redirect:/role/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        roleService.deleteById(id);
        return "redirect:/role/list";
    }


}
